package geolabo;

import org.w3c.dom.*;
import java.util.*;

/*
 * Angleimage.java
 *
 * Created on 11 septembre 2005, 18:15
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author Fred
 */
public class Angleimage extends Anglegeo{
    
  Transformation t;
  Anglegeo a;  

  public Angleimage(String nom, geopalette parent, Geoaspectangle gaspect,
                  Anglegeo a, Transformation t,int angle) {
    super(nom, parent, gaspect);
    this.a = a;
    this.t = t;
    if (parent != null) {
      a.inseredependance(this);
      t.inseredependance(this);
    }
    p1 = new Pointimage(null, parent, null, a.getp1(), t);
    p2 = new Pointimage(null, parent, null, a.getp2(), t);
    p3 = new Pointimage(null, parent, null, a.getp3(), t);
    this.angle=angle;
    this.rapport=a.getrapport();
    calcule();
  }    
    
  public Angleimage(geopalette parent, Element elt, GeoVector objets, int angle) {
    this.parent = parent;
    this.type = ANGLE;
    this.ismovable = true;
    this.angle = angle;

    /* On parcourt les enfants pour retrouver le Geoaspectpoint */

    NodeList list = elt.getChildNodes();
    Node n;
    for (int i = 0; i < list.getLength(); i++) {
      n = list.item(i);
      if (n instanceof Element) {
        if ( ( (Element) n).getTagName().equals("GEOASPECTANGLE")) {
          gaspect = new Geoaspectangle( (Element) n);
        }
      }
    }
    setfictif(elt);
    
    nom = elt.getAttribute("nom");
    rapport = Double.parseDouble(elt.getAttribute("rapport"));
    a = (Anglegeo) objets.getbyname(elt.getAttribute("a"));
    t = (Transformation) objets.getbyname(elt.getAttribute("t"));

    if ( (a != null) && (t != null) && (parent != null)) {
      a.inseredependance(this);
      t.inseredependance(this);    
      p1 = new Pointimage(null, parent, null, a.getp1(), t);
      p2 = new Pointimage(null, parent, null, a.getp2(), t);
      p3 = new Pointimage(null, parent, null, a.getp3(), t);
      this.angle=angle;
      calcule();
    }
  }
  
// Construction dans le cadre d'une macro...
  public Angleimage(geolabpalette parent, Element elt, Hashtable tabnom,String nom) {
    this.parent = parent;
    this.type = ANGLE;
    this.ismovable = true;
    this.angle = parent.gdm.angle;      
    
    gaspect=new Geoaspectangle(parent.geoaspect.getaspectangle());

    this.nom=nom;
    macro=true;

    a = (Anglegeo) tabnom.get(elt.getAttribute("a"));
    t = (Transformation) tabnom.get(elt.getAttribute("t"));   
    rapport = Double.parseDouble(elt.getAttribute("rapport"));   
    
   if ( (a != null) && (t != null) && (parent != null)) {
      a.inseredependance(this);
      t.inseredependance(this);    
      p1 = new Pointimage(null, parent, null, a.getp1(), t);
      p2 = new Pointimage(null, parent, null, a.getp2(), t);
      p3 = new Pointimage(null, parent, null, a.getp3(), t);
      this.angle=angle;
      calcule();
    }    
  }
  
  
  public Element sauve(Document doc) {
    Element elt = doc.createElement("ANGLEIMAGE");

    elt.appendChild(gaspect.sauve(doc));
    elt.setAttribute("t", t.getshortname());
    elt.setAttribute("nom", nom);
    elt.setAttribute("a", a.getshortname());
    elt.setAttribute("rapport",""+rapport);
    elt.setAttribute("fictif",""+fictif);

    return elt;
  }  
  
  public Element sauvemacro(Document doc,Hashtable tabnom,String mynom)
  {
    Element elt = doc.createElement("ANGLEIMAGE");

    elt.setAttribute("t",(String) tabnom.get(t));
    elt.setAttribute("a",(String) tabnom.get(a));
    elt.setAttribute("rapport",""+rapport);    
    elt.setAttribute("nom",mynom);

    return elt;
  }         
  
}
